/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.transaction.panel;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JPanel;
import org.balisunrise.builder.BuilderException;
import org.balisunrise.swing.builder.LayoutBuilder;

/**
 * Painel composto por uma lista de paineis.
 *
 * @author Glauco
 */
public class MultiPanel extends JPanel{

    /**
     * Lista de paineis que compõe este.
     */
    private List<JPanel> panels;

    /**
     * Contrutor do layout do painel dos campos de uma entidade.
     */
    private LayoutBuilder layoutBuilder;

    /**
     * Construtor do MultiPanel.
     * É construido o layout após as atribuições.
     *
     * @param panels paineis que compõe este.
     * @param layoutBuilder construtor do layout deste painel.
     */
    public MultiPanel(List<JPanel> panels, LayoutBuilder layoutBuilder) {
        this.panels = panels;
        this.layoutBuilder = layoutBuilder;
        createLayout();
    }

    /**
     * get the layoutBuilder.
     * @return the layoutBuilder.
     */
    public LayoutBuilder getLayoutBuilder() {
        return layoutBuilder;
    }

    /**
     * set the layoutBuilder
     * @param layoutBuilder
     */
    public void setLayoutBuilder(LayoutBuilder layoutBuilder) {
        this.layoutBuilder = layoutBuilder;
    }

    /**
     * get the list of panels
     * @return the list of panels
     */
    public List<JPanel> getPanels() {
        return panels;
    }

    /**
     * set the the list of panels
     * @param panels
     */
    public void setPanels(List<JPanel> panels) {
        this.panels = panels;
    }

    /**
     * Cria o layout deste painel.
     * @see #panels
     * @see #layoutBuilder
     */
    public void createLayout(){

        try{
            // lista dos FieldLayout's
            List<JComponent> aux = new ArrayList<JComponent>();
            // Carrega lista
            for (JPanel p : panels) {
                aux.add(p);
            }
            // controi layout
            layoutBuilder.create(this, aux);
        }catch(BuilderException be){
            System.out.println(
                    "Erro em SearchPanel ao criar layout.\n"+
                    be.getMessage());
        }
    }
}
